package com.crm.miaohe.controller.config;

import com.crm.miaohe.controller.config.request.ConfigUpsertRequest;
import com.crm.miaohe.entity.dto.ConfigEntity;
import com.crm.miaohe.service.ConfigService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/erp/config")
public class ConfigController {

    private final ConfigService configService;

    public ConfigController(ConfigService configService) {
        this.configService = configService;
    }

    @GetMapping
    public ResponseEntity<List<ConfigEntity>> getConfigs() {
        return ResponseEntity.ok(configService.getConfigs());
    }

    @GetMapping("/{configKey}")
    public ResponseEntity<ConfigEntity> getConfig(@PathVariable String configKey) {
        return ResponseEntity.ok(configService.getConfig(configKey));
    }

    @PostMapping
    public ResponseEntity<Void> addConfig(@RequestBody ConfigUpsertRequest request) {
        configService.addConfig(request);
        return ResponseEntity.ok().build();
    }

    @PutMapping
    public ResponseEntity<Void> updateConfig(@RequestBody ConfigUpsertRequest request) {
        configService.updateConfig(request);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{configKey}")
    public ResponseEntity<Void> deleteConfig(@PathVariable String configKey) {
        configService.deleteConfig(configKey);
        return ResponseEntity.ok().build();
    }


}
